using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using FeedGatheringService.Properties;
using Rss;
//using FeedGatheringService.Properties;

namespace FeedGatheringService
{
    [Serializable]
    public class Post : BaseEntity
    {
        #region Fields

        private string title;
        private DateTime postDate;
        private string link;
        private string body;
        private string author;
        private int feedID;

        #endregion

        #region Constructors

        public Post()
        {
        }

        public Post(int id, DateTime createdDate, string title, DateTime postDate, string link, string body, string author, int feedID, string lastPostGuidName) : base(id, createdDate)
        {
            this.title = title;
            this.postDate = postDate;
            this.link = link;
            this.body = body;
            this.author = author;
            this.feedID = feedID;
        }

        #endregion

        #region Properties

        public int FeedID
        {
            get { return feedID; }
            set { feedID = value; }
        } 

        public string Author
        {
            get { return author; }
            set { author = value; }
        } 

        public string Body
        {
            get { return body; }
            set { body = value; }
        } 

        public string Link
        {
            get { return link; }
            set { link = value; }
        } 
        
        public DateTime PostDate
        {
            get { return postDate; }
            set { postDate = value; }
        } 
        
        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        #endregion

        #region Methods

        public void Add()
        {
            using (SqlConnection cn = new SqlConnection(Settings.Default.BlogRollConnectionString))
            {
                string sql = "INSERT INTO Post (	FeedID, Title, PostDate, Author, Body, Link  )	VALUES ( @feedID, @title, @postDate, @author, @body, @link )";
                using (SqlCommand cmd = new SqlCommand(sql, cn))
                {
                    cn.Open();
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@feedID", this.feedID);
                    cmd.Parameters.AddWithValue("@title", this.title);
                    cmd.Parameters.AddWithValue("@postDate", this.postDate);
                    cmd.Parameters.AddWithValue("@author", this.author);
                    cmd.Parameters.AddWithValue("@body", this.body);
                    cmd.Parameters.AddWithValue("@link", this.link);
                    cmd.ExecuteNonQuery();
                    cn.Close();
                }
            }
        }

        #endregion

        #region Static Methods

        public static List<Post> GetNewRssPostsFromUrl(Feed feed)
        {
            string mostRecentPostGuidName = null;
            List<Post> postList = new List<Post>();
            RssFeed rssFeed = RssFeed.Read(feed.Url);
            if (rssFeed.Channels.Count == 0)
                return postList;

            // Get Channel 0
            RssChannel channel = rssFeed.Channels[0];

            foreach (RssItem rssItem in channel.Items)
            {
                // If we already have this post, get out. This means the rest of the posts are old.
                if (feed.LastPostGuidName == rssItem.Guid.Name)
                    break;

                // Grab the first Post's Guid
                if (mostRecentPostGuidName == null) mostRecentPostGuidName = rssItem.Guid.Name;

                Post post = new Post();
                post.FeedID = feed.Id;
                post.Author = rssItem.Author;
                post.Link = rssItem.Link.ToString();
                post.PostDate = rssItem.PubDate;
                post.Body = rssItem.Description;
                post.Title = rssItem.Title;
                postList.Add(post);
            }

            // Update the Feed's last post setting
            if (mostRecentPostGuidName != null)
                feed.LastPostGuidName = mostRecentPostGuidName;

            return postList;
        }


        #endregion
    }
}
